﻿using System;
using System.Windows.Input;
using RIA.ServicesLibrary.Web.Services;
using System.ServiceModel.DomainServices.Client;
using System.Linq;
using RIA.ServicesLibrary.Web.Models;

namespace RIA.ViewModel
{
    public class ProductDetailsViewModel:ViewModelBase
    {
        #region Declarations
        Product _product;
        ProductContext _context = new ProductContext ();
        #endregion //Declarations


        #region Properties

        public Product Product
        {
            get { return _product; }
            set
            {
                _product = value;
                OnPropertyChanged ( "Product" );
            }
        }

        #endregion //Properties


        #region LoadProductCommand
        ICommand _loadProductCommand;

        public ICommand LoadProductCommand
        {
            get
            {
                if ( _loadProductCommand == null )
                {
                    _loadProductCommand = new DelegateCommand ( param => LoadProductExecute ( param ), CanLoadProductExecute );
                }
                return _loadProductCommand;
            }
        }
        bool CanLoadProductExecute ( object param )
        {
            return true;
        }

        public void LoadProductExecute ( object param )
        {
            int productid = ( int ) param;
            ShowBusyStatus ();
            EntityQuery<Product> qry = _context.GetProductsQuery ();
            qry = qry.Where<Product> ( c => c.ProductID == productid );
            LoadOperation<Product> op = _context.Load ( qry, true );
            op.Completed += ( x, y ) =>
            {
                HideBusyStatus ();
                if ( op.HasError )
                {
                    System.Windows.MessageBox.Show ( op.Error.ToString (), "Load Error", System.Windows.MessageBoxButton.OK );
                    op.MarkErrorAsHandled ();
                }
                else
                {
                    if ( op.Entities.Count () != 0 )
                    {
                        this.Product = op.Entities.First ();
                    }
                }
            };
        } 
        #endregion

       
        

        #region SaveProductCommand
        ICommand _saveProductCommand;

        public ICommand SaveProductCommand
        {
            get
            {
                if ( _saveProductCommand == null )
                {
                    _saveProductCommand = new DelegateCommand ( SaveProductExecute, param => CanSaveProductExecute ( param ) );
                }
                return _saveProductCommand;
            }
        }



        bool CanSaveProductExecute ( object param )
        {
            return true;
        }

        public void SaveProductExecute ( object param )
        {
            if (_product != null )
            {
                this.ShowBusyStatus ();
                var op = _context.SubmitChanges ();
                op.Completed += SaveCustomer_Completed;
            }
        }
        private void SaveCustomer_Completed ( object sender, EventArgs e )
        {
            this.HideBusyStatus ();
            SubmitOperation op = sender as SubmitOperation;

            if ( !op.HasError )
            {
                // You may wish to notify the View that the save is complete
                // by raising an event here, and responding to it in the View
                RaiseOnSavedData ();
            }
            else
            {
                DomainOperationException error = op.Error as DomainOperationException;
                switch ( error.Status )
                {
                    case OperationErrorStatus.Conflicts:
                        // Handle concurrency violations
                        // Loop through the entities with concurrency violations
                        break;
                    case OperationErrorStatus.ServerError:
                        // Handle server errors
                        break;
                    case OperationErrorStatus.Unauthorized:
                        // Handle unauthorized domain operation access
                        break;
                    case OperationErrorStatus.ValidationFailed:
                        // Handle validation rule failures
                        break;
                    default:

                        break;
                }
                System.Windows.MessageBox.Show ( op.Error.ToString (), "update Error", System.Windows.MessageBoxButton.OK );
                op.MarkErrorAsHandled ();
           }
        }

        #endregion


        private Product CreateProduct()
        {
            Product newProduct = new Product ();
            newProduct.rowguid = Guid.NewGuid ();
            newProduct.ModifiedDate = DateTime.Now;
            newProduct.SellStartDate = DateTime.Now;
            return newProduct;
        }

        public void AddNewProduct()
        {
            this.Product = CreateProduct ();
            _context.Products.Add (this.Product);
        }



    }
}
